1 /* Complexity: O(E + V)
2 Tarjan's algorithm for finding strongly connected
5 *d[i] = Discovery time of node i. (Initialize to -1)
6 *low[i] = Lowest discovery time reachable from node
7 i. (Doesn't need to be initialized)
8 *scc[i] = Strongly connected component of node i. (Doesn't
9 need to be initialized)
10 *s = Stack used by the algorithm (Initialize to an empty
12 *stacked[i] = True if i was pushed into s. (Initialize to
14 *ticks = Clock used for discovery times (Initialize to 0)
15 *current_scc = ID of the current_scc being discovered
19 int d
[MAXN
], low
[MAXN
], scc
[MAXN
];
22 int ticks
, current_scc
;
24 d
[u
] = low
[u
] = ticks
++;
27 const vector
<int> &out
= g
[u
];
28 for (int k
=0, m
=out
.size(); k
<m
; ++k
){
29 const int &v
= out
[k
];
32 low
[u
] = min(low
[u
], low
[v
]);
33 }else if (stacked
[v
]){
34 low
[u
] = min(low
[u
], low
[v
]);